package com.dw.etl.core.controller;

import javax.servlet.http.HttpServletRequest;

import com.dw.etl.common.base.BaseException;
import com.dw.etl.core.bean.model.EtlBaseField;
import com.dw.etl.core.bean.vo.LayMsg;
import org.springframework.beans.factory.annotation.Autowired;
import  com.dw.etl.common.base.BaseResult;
import  com.dw.etl.common.constant.CommonResultConstant;
import com.dw.etl.core.api.MdMedTbFdService;
import com.dw.etl.core.bean.model.MdMedTbFd;
import org.springframework.util.StringUtils;
import  com.dw.etl.common.vo.PaginationVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * mdMedTbFd控制器
 * Created by hozeData on 2021/06/17.
 */
@RestController
@RequestMapping("/etl/mdMedTbFd/")
@Api(description="开发中", tags="开发中")
@CrossOrigin(origins = "*", maxAge = 3600)
public class MdMedTbFdController {

    @Autowired
    MdMedTbFdService mdMedTbFdService;

    @ApiOperation(value="分页查询")
    @RequestMapping(value="findByPage", method=RequestMethod.POST)
    public BaseResult findByPage(@RequestBody MdMedTbFd mdMedTbFd, Integer pageNum, Integer pageSize) {
        PaginationVO<MdMedTbFd> pageVO = mdMedTbFdService.findPageByModel(mdMedTbFd, pageNum, pageSize);
        return new BaseResult(CommonResultConstant.SUCCESS, pageVO);
    }

    @ApiOperation(value="查询")
    @RequestMapping(value="find", method=RequestMethod.POST)
    public LayMsg find(@RequestBody MdMedTbFd mdMedTbFd) {
        List<MdMedTbFd> pageVO = mdMedTbFdService.findByModel(mdMedTbFd);
        return LayMsg.success().addCount(pageVO.size()).addData(pageVO);
    }

    @ApiOperation(value="查询")
    @RequestMapping(value="findTbDimField", method=RequestMethod.GET)
    public LayMsg findTbDimField(String idMedTb) {
        List<MdMedTbFd> pageVO = mdMedTbFdService.findTbDimField(idMedTb);
        return LayMsg.success().addCount(pageVO.size()).addData(pageVO);
    }

    @ApiOperation(value="保存")
    @RequestMapping(value="saveList", method=RequestMethod.POST)
    public BaseResult saveList(@RequestBody List<MdMedTbFd> fieldList, HttpServletRequest request)  {
        mdMedTbFdService.save(fieldList);
        MdMedTbFd field = new MdMedTbFd();
        field.setIdMedTb(fieldList.get(0).getIdMedTb());
        List<MdMedTbFd> pageVO = mdMedTbFdService.findByModel(field);
        return new BaseResult(CommonResultConstant.SUCCESS, pageVO);
    }

    @ApiOperation(value="保存")
    @ApiImplicitParam(name="mdMedTbFd", value="实体", paramType="body", required=true, dataType="MdMedTbFd")
    @RequestMapping(value="insert", method=RequestMethod.POST)
    public BaseResult insert(@RequestBody MdMedTbFd mdMedTbFd, HttpServletRequest request)  {
        int num = mdMedTbFdService.insertSelective(mdMedTbFd);
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }

    @ApiOperation(value="详情")
    @ApiImplicitParam(name="idMedTbFd", value="主键", paramType="query", required=true, dataType="String")
    @RequestMapping(value="getById", method=RequestMethod.GET)
    public BaseResult getById(String idMedTbFd, HttpServletRequest request) throws BaseException {
        if(StringUtils.isEmpty(idMedTbFd)){
            throw new BaseException("请选择要编辑的项");
        }
        MdMedTbFd mdMedTbFd = mdMedTbFdService.getById(idMedTbFd);
        return new BaseResult(CommonResultConstant.SUCCESS, mdMedTbFd);
    }

    @ApiOperation(value="修改")
    @ApiImplicitParam(name="mdMedTbFd", value="实体", paramType="body", required=true, dataType="MdMedTbFd")
    @RequestMapping(value="update", method=RequestMethod.POST)
    public BaseResult update(@RequestBody MdMedTbFd mdMedTbFd, HttpServletRequest request)  {
        int num = mdMedTbFdService.updateByIdSelective(mdMedTbFd);
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }

    @ApiOperation(value="根据主键删除")
    @ApiImplicitParam(name="idMedTbFd", value="主键", paramType="query", required=true, dataType="String")
    @RequestMapping(value="delete", method=RequestMethod.GET)
    public BaseResult delete(String idMedTbFd, HttpServletRequest request) throws BaseException {
        if(StringUtils.isEmpty(idMedTbFd)){
            throw new BaseException("请选择要删除的项");
        }
        int num = mdMedTbFdService.deleteById(idMedTbFd);
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }



}
